/*#include <stdio.h>
int main()
{
    int i;
    int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int *pa[3] = {a[0], a[1], a[2]};
    int *p = a[0];
    for (i = 0; i < 3; i++)
        printf("%d %d %d", a[i][2 - i], *a[i], *(*a + i) + i);
    printf("\n");
    for (i = 0; i < 3; i++)
        printf("%d %d", *pa[i], p[i]);
    printf("\n");
    return 0;
}*/

/*#include <stdio.h>
int main()
{
    int i;
    char a[10], b[10];
    char *st[] = {"one", "two", "three", "four"};
    printf("%s#", *st);
    for (i = 0; i < 4; i++)
        b[i] = *(st[i] + 1);
    b[i] = '\0';
    puts(b);
    return 0;
}*/

/*#include <stdio.h>
int main(void)
{
    int i, flag = 0;
    char ch;
    const char *color[2] = {"blue", "green"};
    const char **pc;

    pc = color;
    for (i = 0; i < 2; i++)
    {
        printf("%s %c\n", *(pc + i), **(pc + i)); /* 输出数据之间间隔一个空格 */
/*    }

    for (i = 0; i < 2; i++)
    {
        printf("%s %c\n", *(pc + i) + 1, **(pc + i) + 1); /* 输出数据之间间隔一个空格 */
   /* }

    return 0;
}*/

/*#include <stdio.h>
int main()
{
    const char *s[3] = {"point", "continue", "break"};

    for (int i = 2; i >= 0; i--)
    {
        for (int j = 2; j > i; j--)
        {
            printf("%s\n", s[i] + j);
        }
    }
}*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define N 20

char **InputDrugName(unsigned int n);
void OrganizeDrugs(char **Drugstrs, unsigned int n);
char *OrganizeOneDrugname(char *Drugname);
void PrintDrug(char **Drugstrs, unsigned int n);
void FreeDrug(char **Drugstrs, unsigned int n);

int main()
{
    unsigned int n;
    printf("Please input number of drugs:\n");
    scanf("%u", &n);
    char **DrugStrs = InputDrugName(n);
    OrganizeDrugs(DrugStrs, n);
    printf("After Organized:\n");
    PrintDrug(DrugStrs, n);
    FreeDrug(DrugStrs, n);
    return 0;
}

char **InputDrugName(unsigned int n)
{
     char **Drugstrs = (char **)malloc(n * sizeof(char *));
     if (Drugstrs == NULL)
    {
         fprintf(stderr, "Memory allocation failed\n");
         exit(1);
    }
     for (unsigned int i = 0; i < n; i++)
    {
         char buffer[N + 1]; // +1 for null terminator
         printf("Please input drug name %u (max %d characters):\n", i + 1, N);
         if (fgets(buffer, sizeof(buffer), stdin) != NULL)
        {
            // Remove newline character if present
             size_t len = strlen(buffer);
             if (len > 0 && buffer[len - 1] == '\n')
            {
                 buffer[len - 1] = '\0';
            }
            // Allocate memory for the drug name string
           Drugstrs[i] = (char *)malloc((strlen(buffer) + 1) * sizeof(char));
           if (Drugstrs[i] == NULL)
            {
                 fprintf(stderr, "Memory allocation failed\n");
                 exit(1);
            }
            strcpy(Drugstrs[i], buffer);
            
        }
        else
        {
            fprintf(stderr, "Error reading input\n");
            exit(1);
        }
    }
    return Drugstrs;
}

char *OrganizeOneDrugname(char *Drugname)
{
    if (Drugname == NULL || *Drugname == '\0')
    {
        return Drugname;
    }
    char *result = strdup(Drugname); // Duplicate the string to avoid modifying the original
    if (result == NULL)
    {
         fprintf(stderr, "Memory allocation failed\n");
         exit(1);
    }
    if (isalpha((unsigned char)result[0]))
    {
         result[0] = toupper((unsigned char)result[0]);
    }
     for (size_t i = 1; result[i] != '\0'; i++)
    {
         if (isalpha((unsigned char)result[i]))
        {
             result[i] = tolower((unsigned char)result[i]);
        }
    }
     return result;
}

void PrintDrug(char **Drugstrs, unsigned int n)
{
     for (unsigned int i = 0; i < n; i++)
    {
         printf("Drug %u: %s\n", i + 1, Drugstrs[i]);
    }
}

void FreeDrug(char **Drugstrs, unsigned int n)
{
     if (Drugstrs != NULL)
    {
         for (char **p = Drugstrs; p < Drugstrs + n; p++)
        {
             if (*p != NULL)
            {
                 free(*p);
            }
        }
         free(Drugstrs);
    }
}
